package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.other.Param;
import m.html.list.ListButton;
import m.html.list.ListDropdown;
import m.html.list.ListSortOrder;
import m.html.list.SuccessMethod;
import m.system.cache.CacheUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminGroup;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;
import manage.service.AdminGroupService;
import manage.util.DictionaryUtil;

import java.util.ArrayList;

@ActionMeta(name="manage/adminGroup")
public class AdminGroupAction extends StatusAction {
	public AdminGroup model;
	private String adminOid;

	/**
	 * 获取我兼职的机构部门
	 */
	public JSONMessage getMyOrgGroup() {
		JSONMessage result=new JSONMessage();
		try {
			AdminLogin admin=getSessionAdmin();
			if(null==admin) throw noLoginException;
			if(null==getSessionAssist()) {
				result.push("list", getService(AdminGroupService.class).getOrgList(admin.getOid()));
			}else{
				result.push("list",new ArrayList<String>());
			}
			fillJSONResult(result, true, "");
		} catch (Exception e) {
			fillJSONResult(result, false, e.getMessage());
		}
		return result;
	}
	/**
	 * 设置我当前兼职的机构部门
	 */
	public JSONMessage setCurrentOrgGroup() {
		JSONMessage result=new JSONMessage();
		try {
			AdminLogin admin=getSessionAdmin();
			if(null==admin) throw noLoginException;
			admin.setOrgGroup(new OrgGroupView());
			admin.getOrgGroup().setOid(model.getOid());
			ModelUpdateUtil.updateModel(admin, "orgGroup.oid");
			setSessionOrgGroupId(model.getOid());
			CacheUtil.clear(AdminLogin.class, admin.getOid());
			fillJSONResult(result, true, "");
		} catch (Exception e) {
			fillJSONResult(result, false, e.getMessage());
		}
		return result;
	}
	public JSONMessage getList() {
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			result.push("list", getService(AdminGroupService.class).getList(model));
			fillJSONResult(result, true, "");
		} catch (Exception e) {
			fillJSONResult(result, false, e.getMessage());
		}
		return result;
	}
	public JSONMessage getGroup4Admin() {
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			result.push("groupList", getService(AdminGroupService.class).getList4Admin(adminOid));
			fillJSONResult(result, true, "");
		} catch (Exception e) {
			fillJSONResult(result, false, e.getMessage());
		}
		return result;
	}

	/**
	 * 保存
	 */
	public JSONMessage doSave(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			String msg=getService(AdminGroupService.class).save(model);
			result.push("model", model);
			fillJSONResult(result,true,msg);
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_system_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())){
			model=ModelQueryList.getModel(model,"*");
		}else if(null==model){
			model=new AdminGroup();
			model.setBusiness("B");
			model.setType("A");
		}
		if(StringUtil.isSpace(model.getBusiness())) model.setBusiness("B");
		if(null!=model.getParent()) {
			model.setParent(ModelQueryList.getModel(model.getParent(), "*"));
		}
		return new VuePageResult("page/base/editAdminGroup.js")
			.addParam("model",model);
	}
//	public VueResult selectAdminGroupPage() throws Exception {
//		boolean power=hasAdminOperPower(getStatusPower());
//		VueListResult result=getVueListResult("选择管理员组",AdminGroup.class,"action/manage/adminGroup/selectAdminGroupPage");
//		result.setFunction(true,true);
//		result.setSelection("oid", true,"record.oid=='"+getParamsValue("selected")+"'");
//		result.addCol(toIndexColumn());
//		result.addCol(toTextColumn("名称", "name",true));
//		result.addCol(toTextColumn("编号", "num",true).setSort(true));
//		result.addCol(toTextColumn("类型", "business",true).setDictOption(AdminGroup.class,"business"));
//		result.addCol(toTextColumn("描述", "description",150,true));
//		result.addCol(toTextColumn("排序", "sort",50).setSort(ListSortOrder.DESC));
//		result.addCol(toStatusColumn(power,true));
//		result.addButton(ListButton.back("选择",ButtonType.PRIMARY).addParam("",""));
//
//		result.addCondition(QueryCondition.eq("type", "A"));
//		return result;
//	}
	/**
	 * 权限组设置
	 */
	public VueResult adminGroupList() throws Exception {
		verifyMenu("A1101");
		boolean power=hasAdminOperPower(getStatusPower());
		VueListResult result=getVueListResult("管理员组",AdminGroup.class,"action/manage/adminGroup/adminGroupList");
		result.setSubTitle("编辑管理员组，配置管理员组权限。");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("名称", "name",true));
		result.addCol(toTextColumn("编号", "num",true).setSort(true));
		//result.addCol(toTextColumn("类型", "business",true).setDictOption(AdminGroup.class,"business"));
		result.addCol(toTextColumn("描述", "description",150,true));
		result.addCol(toTextColumn("排序", "sort",50).setSort(ListSortOrder.DESC));
		result.addCol(toStatusColumn(power,true));
		if(power) result.addCol(toSpaceColumn("操作",200,
			ListButton.modal("修改", ButtonType.PRIMARY,"action/manage/adminGroup/toEdit",700, SuccessMethod.REFRESH)
				.addParam("model.oid","oid"),
			ListDropdown.def("权限").add(
				ListButton.modal("菜单设置",ButtonType.DEFAULT,"page/base/setGroupMenuPage.js",700)
					.addParam("groupOid", "oid"),
				ListButton.modal("权限设置",ButtonType.DEFAULT,"page/base/setAdminGroupPowerPage.js",400)
					.addParam("groupOid", "oid")
			),
			ListButton.page("管理员",ButtonType.LINK,"action/manage/adminLogin/adminLoginList")
				.addParam("params[adminGroupOid]", "oid")
		).setFixed(Align.RIGHT));
		if(power) result.addButton(ListButton.modal("添加",ButtonType.PRIMARY,"action/manage/adminGroup/toEdit",700,SuccessMethod.REFRESH)
				.addQueryParam(Param.value("model.type","A")).addQueryParam(Param.value("model.business", "B")));

		result.addCondition(QueryCondition.eq("type", "A"));
		return result;
	}
	/**
	 * 角色列表设置
	 * 
	 *  Exception
	 */
	public VueResult adminRoleList() throws Exception {
		verifyMenu("A1103");
		boolean power=hasAdminOperPower(getStatusPower());
		VueListResult result=getVueListResult("角色设置",AdminGroup.class,"action/manage/adminGroup/adminRoleList");
		result.setSubTitle("编辑角色组，配置角色组权限。");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("名称", "name",true));
		result.addCol(toTextColumn("编号", "num",true).setSort(true));
		result.addCol(toTextColumn("类型", "business").setDictOption(AdminGroup.class,"business"));
		result.addCol(toTextColumn("描述", "description",true));
		result.addCol(toTextColumn("排序", "sort",50).setSort(ListSortOrder.DESC));
		result.addCol(toStatusColumn(power,true));
		if(power) result.addCol(toSpaceColumn("操作",220,
			ListButton.modal("修改",ButtonType.LINK,"action/manage/adminGroup/toEdit",700,SuccessMethod.REFRESH)
				.addParam("model.oid","oid"),
			ListButton.modal("关联用户",ButtonType.DEFAULT,"page/base/setAdminGroupLinkPage.js",850,SuccessMethod.REFRESH)
				.addParam("adminGroupOid","oid"),
			ListDropdown.def("权限").add(
				ListButton.modal("菜单设置",ButtonType.DEFAULT,"page/base/setGroupMenuPage.js",700)
					.addParam("groupOid", "oid"),
				ListButton.modal("权限设置",ButtonType.DEFAULT,"page/base/setAdminGroupPowerPage.js",400)
					.addParam("groupOid", "oid")
			)
		));
		if(power) result.addButton(ListButton.modal("添加",ButtonType.PRIMARY,"action/manage/adminGroup/toEdit",700,SuccessMethod.REFRESH)
				.addQueryParam(Param.value("model.type","B")).addQueryParam(Param.value("model.business", "B")));
		
		result.addCondition(QueryCondition.eq("type", "B"));
		return result;
	}

	@Override
	public void doRecoveryCallback() {
		DictionaryUtil.clear(AdminGroup.class);
		DictionaryUtil.clear(OrgGroupView.class);
	}

	@Override
	public void doDisableCallback() {
		DictionaryUtil.clear(AdminGroup.class);
		DictionaryUtil.clear(OrgGroupView.class);
	}

	@Override
	public String[] getStatusPower() {
		return new String[]{"manage_system_power"};
	}

	public String getAdminOid() {
		return adminOid;
	}
	public void setAdminOid(String adminOid) {
		this.adminOid = adminOid;
	}
	public AdminGroup getModel() {
		return model;
	}

	public void setModel(AdminGroup model) {
		this.model = model;
	}

}
